package com.glueball.crm.module.client.ui;

import com.glueball.crm.module.client.model.IdentificationType;
import com.glueball.crm.module.client.model.Person;
import com.glueball.crm.module.client.model.PersonName;
import com.glueball.crm.module.common.ui.CrmDateWidget;
import com.glueball.crm.module.common.ui.CrmWidget;
import com.glueball.crm.module.common.ui.EditableWidget.StartView;
import com.glueball.crm.module.common.ui.KeyValueWidget;
import com.glueball.crm.module.common.ui.PersonNameWidget;
import com.glueball.crm.module.util.DateUtil;
import com.glueball.crm.module.util.KeyValue;
import com.glueball.crm.module.util.URLHelper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class PersonWidget extends CrmWidget {
	
	@UiField SimplePanel namePanel;
	@UiField HorizontalPanel nameCtrlPanel;
	@UiField SimplePanel maidenNamePanel;
	@UiField HorizontalPanel maidenNameCtrlPanel;
	@UiField SimplePanel mothersNamePanel;
	@UiField HorizontalPanel mothersNameCtrlPanel;
	@UiField SimplePanel placeOfBirthPanel;
	@UiField HorizontalPanel placeOfBirthCtrlPanel;
	@UiField SimplePanel dateOfBirthPanel;
	@UiField HorizontalPanel dateOfBirthCtrlPanel;
	@UiField SimplePanel identificationTypePanel;
	@UiField HorizontalPanel identificationTypeCtrlPanel;
	@UiField SimplePanel taxIdPanel;
	@UiField HorizontalPanel taxIdCtrlPanel;

	private final String clientId;
	private final Person entity;
	
	private static PersonWidgetUiBinder uiBinder = GWT.create(PersonWidgetUiBinder.class);

	interface PersonWidgetUiBinder extends UiBinder<Widget, PersonWidget> {
	}

	private PersonNameWidget nameWidget;
	private PersonNameWidget mothersNameWidget;
	private PersonNameWidget maidenNameWidget;
	private KeyValueWidget   placeOfBirthWidget;
	private CrmDateWidget    dateOfBirthWidget;
	private KeyValueWidget   identificationTypeWidget;
	private KeyValueWidget   taxIdWidget;
	
	public PersonWidget(final Person entity, final Composite parent, final String clientId) {
		this.entity = entity;
		this.clientId = clientId;
		this.widget = uiBinder.createAndBindUi(this);
		init();
	}

	public void init() {
		this.nameWidget = new PersonNameWidget(
				this.entity.getName() == null ? new PersonName() : this.entity.getName(),
				namePanel,
				nameCtrlPanel,
				StartView.SHOW,
				URLHelper.SAVE_CLIENT_PERSON_NAME + this.clientId);

		this.maidenNameWidget = new PersonNameWidget(
				this.entity.getMaidenName()==null ? new PersonName() :	this.entity.getMaidenName(),
				maidenNamePanel,
				maidenNameCtrlPanel,
				StartView.SHOW,
				URLHelper.SAVE_CLIENT_PERSON_MAIDENNAME + this.clientId);

		this.mothersNameWidget = new PersonNameWidget(
				this.entity.getMothersName() == null ? new PersonName() : this.entity.getMothersName(),
				mothersNamePanel,
				mothersNameCtrlPanel,
				StartView.SHOW,
				URLHelper.SAVE_CLIENT_PERSON_MOTHERSNAME + this.clientId);

		this.placeOfBirthWidget = new KeyValueWidget(
				new KeyValue("placeOfBirth", this.entity.getPlaceOfBirth() == null ? "" : this.entity.getPlaceOfBirth()),
				placeOfBirthPanel,
				placeOfBirthCtrlPanel,
				StartView.SHOW,
				URLHelper.MODIFY_CLIENT_DATA + this.clientId);

		this.dateOfBirthWidget = new CrmDateWidget(
				this.entity.getDateOfBirth() == null ? DateUtil.newCrmDate() : this.entity.getDateOfBirth(),
				dateOfBirthPanel,
				dateOfBirthCtrlPanel,
				StartView.SHOW,
				URLHelper.SAVE_CLIENT_PERSON_DATEOFBITH + this.clientId);

		this.identificationTypeWidget = new KeyValueWidget(
				new KeyValue("identificationType",
						this.entity.getIdentificationType() == null ?
						IdentificationType.IDCARD.toString() : this.entity.getIdentificationType().toString()),
				identificationTypePanel,
				identificationTypeCtrlPanel,
				StartView.SHOW,
				URLHelper.MODIFY_CLIENT_DATA + this.clientId);

		this.taxIdWidget = new KeyValueWidget(
				new KeyValue("taxId", this.entity.getTaxId() == null ? "" : this.entity.getTaxId()),
				taxIdPanel,
				taxIdCtrlPanel,
				StartView.SHOW, 
				URLHelper.MODIFY_CLIENT_DATA + this.clientId);
	}

	public PersonNameWidget getMothersNameWidget() {
		return mothersNameWidget;
	}

	public void setMothersNameWidget(PersonNameWidget mothersNameWidget) {
		this.mothersNameWidget = mothersNameWidget;
	}

	public PersonNameWidget getMaidenNameWidget() {
		return maidenNameWidget;
	}

	public void setMaidenNameWidget(PersonNameWidget maidenNameWidget) {
		this.maidenNameWidget = maidenNameWidget;
	}

	public KeyValueWidget getPlaceOfBirthWidget() {
		return placeOfBirthWidget;
	}

	public void setPlaceOfBirthWidget(KeyValueWidget placeOfBirthWidget) {
		this.placeOfBirthWidget = placeOfBirthWidget;
	}

	public CrmDateWidget getDateOfBirthWidget() {
		return dateOfBirthWidget;
	}

	public void setDateOfBirthWidget(CrmDateWidget dateOfBirthWidget) {
		this.dateOfBirthWidget = dateOfBirthWidget;
	}

	public KeyValueWidget getIdentificationTypeWidget() {
		return identificationTypeWidget;
	}

	public void setIdentificationTypeWidget(KeyValueWidget identificationTypeWidget) {
		this.identificationTypeWidget = identificationTypeWidget;
	}

	public KeyValueWidget getTaxIdWidget() {
		return taxIdWidget;
	}

	public void setTaxIdWidget(KeyValueWidget taxIdWidget) {
		this.taxIdWidget = taxIdWidget;
	}

	public PersonNameWidget getNameWidget() {
		return nameWidget;
	}

	public void setNameWidget(PersonNameWidget nameWidget) {
		this.nameWidget = nameWidget;
	}
}
